package com.soft.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.soft.admin.common.ElementConst;
import com.soft.dao.CentreDao;
import com.soft.dao.ChannelDao;
import com.soft.front.dto.LocationDTO;
import com.soft.model.Centre;
import com.soft.model.Channel;

@Component
public class CentreDaoImpl extends BaseDaoImpl implements CentreDao {
	private ChannelDao channelDao;
	
	@Autowired
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}

	//	@Cacheable(value = "CENTRE_INFO_CACHE", key = "#id")
	public Centre get(Integer id) {
		return (Centre)this.findUniqueByProperty(Centre.class, "channel.id", id);
	}
	
	@CacheEvict(value = "CENTRE_INFO_CACHE", allEntries = true)
	public void addOrEdit(Centre centre) {
		this.saveOrUpdate(centre);
	}
	
	@CacheEvict(value = "CENTRE_INFO_CACHE", allEntries = true)
	public void remove(Centre centre) {
		this.delete(centre);
	}

	@Cacheable(value = "CENTRE_INFO_CACHE", key = "#index")
	public Map<String, Object> get(String index) {
		Map<String, Object> resMap = new HashMap<String, Object>();
//		//获取栏目信息
		Channel chan = this.channelDao.get(ElementConst.ZXJJCONST);
		Channel indexChannel = this.channelDao.get(index);
		//获取相对应信息.
		Centre centre = this.get(indexChannel.getId());
		resMap.put("title", centre.getTitle());
		resMap.put("txt", centre.getTxt());
		//位置定位
		List<LocationDTO> locationDTO = new ArrayList<LocationDTO>();
		LocationDTO doo = new LocationDTO();
		doo.setName(chan.getName());
		doo.setUrl(ElementConst.ZXJJCONST + "/index.jhtml");
		locationDTO.add(doo);
		if (!"".equals(index)) {
			LocationDTO doo1 = new LocationDTO();
			doo1.setName(indexChannel.getName());
			doo1.setUrl(ElementConst.ZXJJCONST + "/" + index + "/index.jhtml");
			locationDTO.add(doo1);
			resMap.put("url", ElementConst.ZXJJCONST + "/" + index + "/index.jhtml");
			resMap.put("title", indexChannel.getName());
		}
		resMap.put("locations", locationDTO);
		return resMap;
	}

}
